********************************************************************************
* Load datasets
********************************************************************************

foreach co in argentina benin burkinafaso cameroon china dominicanrepublic ecuador elsalvador fiji haiti honduras mali nicaragua panama philippines senegal tanzania uruguay zambia{
	
	* Importing data per subdistrict into Stata
	
	* Importing Rural
	import delimited raw_datasets\IPUMS_labor\urban-rural\rural_`co'.csv, encoding(UTF-8) clear
	* subdist is loaded as strL which prevents merging, this fixes the problem
	gen subdist2 = subdist
	drop subdist
	rename subdist2 subdist
	* save
	tempfile rural_`co'
	save `rural_`co'', replace
	
	* Importing Urban
	import delimited raw_datasets\IPUMS_labor\urban-rural\urban_`co'.csv, encoding(UTF-8) clear
	* subdist is loaded as strL which prevents merging, this fixes the problem
	gen subdist2 = subdist
	drop subdist
	rename subdist2 subdist
	
	* Merging
	merge 1:1 subdist using `rural_`co''
	
	* Cleaning
	drop type_var rowtotal
	drop if subdist == "COL TOTAL"
	
	* Saving
	tempfile urbsh_`co'
	save `urbsh_`co'', replace
}

********************************************************************************
* Calculate rural-urban shares country-by-country
********************************************************************************

*-------------------------------------------------------------------------------
* Argentina

use `urbsh_argentina', clear

foreach urbrur of varlist u* r*{
    replace `urbrur' = 0 if `urbrur' == .
}

* Genearting urban shares

foreach anio in 1980 1991 2001{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_argentina', replace

*-------------------------------------------------------------------------------
* Benin 

use `urbsh_benin', clear

foreach anio in 1979 1992 2002 2013{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_benin', replace

*-------------------------------------------------------------------------------
* Burkina Faso

use `urbsh_burkinafaso', clear

* Cleaning
drop if subdist == ""

foreach anio in 1985 1996 2006{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_burkinafaso', replace

*-------------------------------------------------------------------------------
* Cameroon

use `urbsh_cameroon', clear

foreach anio in 1976 1987 2005{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_cameroon', replace

*-------------------------------------------------------------------------------
* China

use `urbsh_china', clear

foreach anio in 1982 1990 2000{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_china', replace

*-------------------------------------------------------------------------------
* Dominican Republic 

use `urbsh_dominicanrepublic', clear

* Cleaning
drop if subdist == ""
drop if subdist == "214006099 NA"

foreach anio in 1960 1970 1981 2002 2010{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_dominicanrepublic', replace

*-------------------------------------------------------------------------------
* Ecuador

use `urbsh_ecuador', clear

* Cleaning
drop if subdist == "218001099 unknown"
drop if subdist == "218099999 Unknown"

foreach anio in 1962 1974 1982 1990 2001 2010{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_ecuador', replace

*-------------------------------------------------------------------------------
* El Salvador

use `urbsh_elsalvador', clear

foreach anio in 1992 2007{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_elsalvador', replace

*-------------------------------------------------------------------------------
* Fiji

use `urbsh_fiji', clear

* Cleaning
drop if subdist == "242998998 In ships"
drop if subdist == "242999999 Unknown"

foreach anio in 1966 1976 1986 1996 2007 2014{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_fiji', replace

*-------------------------------------------------------------------------------
* Haiti

use `urbsh_haiti', clear

* Cleaning
drop if subdist == "332003029 arrondissement unknown"
drop if subdist == "332006019 arrondissement unknown"
drop if subdist == "332006039 arrondissement unknown"
drop if subdist == "332007049 arrondissement unknown"
drop if subdist == "332009099 arrondissement unknown"

foreach anio in 1971 1982 2003{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_haiti', replace

*-------------------------------------------------------------------------------
* Honduras

use `urbsh_honduras', clear

* Cleaning
drop if subdist == "340009099 Gracias a Dios department, unknown municipality"
drop if subdist == "340099099 Unknown"
drop if subdist == "340012099 La Paz department, unkown municipality"

foreach anio in 1961 1974 1988 2001{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_honduras', replace

*-------------------------------------------------------------------------------
* Mali

use `urbsh_mali', clear

* Cleaning
drop if subdist == "466099999 Unknown"

foreach anio in 1987 1998 2009{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_mali', replace

*-------------------------------------------------------------------------------
* Nicaragua

use `urbsh_nicaragua', clear

foreach anio in 1971 1995 2005{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_nicaragua', replace

*-------------------------------------------------------------------------------
* Panama

use `urbsh_panama', clear

foreach anio in 1960 1970 1980 1990 2000 2010{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_panama', replace

*-------------------------------------------------------------------------------
* Philippines

use `urbsh_philippines', clear

* Cleaning

foreach urbrur of varlist u* r*{
    replace `urbrur' = 0 if `urbrur' == .
}

foreach anio in 1990{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_philippines', replace

*-------------------------------------------------------------------------------
* Senegal

use `urbsh_senegal', clear

foreach anio in 1988 2002 2013{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_senegal', replace

*-------------------------------------------------------------------------------
* Tanzania

use `urbsh_tanzania', clear

foreach anio in 1988 2002 2012{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_tanzania', replace

*-------------------------------------------------------------------------------
* Uruguay

use `urbsh_uruguay', clear

foreach anio in 1963 1975 1985 1996 2006 2011{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_uruguay', replace

*-------------------------------------------------------------------------------
* Zambia

use `urbsh_zambia', clear

foreach anio in 1990 2000 2010{
	
    * Within urban shares
	gen wit_urbsh_`anio' = (u`anio'/(u`anio'+r`anio'))
	la var wit_urbsh_`anio' "Within urban share for `anio'"
	
	* Between urban Shares
	egen toturban_`anio' = total(u`anio')
	gen bet_urbsh_`anio' = (u`anio'/toturban_`anio')
	la var bet_urbsh_`anio' "Between urban share for `anio'"
	
	* Dummy urban share
    gen d_urbsh_80_`anio' = 0
	gen d_urbsh_90_`anio' = 0
    replace d_urbsh_80_`anio' = 1 if wit_urbsh_`anio' >= 0.8
	replace d_urbsh_90_`anio' = 1 if wit_urbsh_`anio' >= 0.9
	la var d_urbsh_80_`anio' "Dummy urban share for `anio' 80% urban pop. cutoff"
	la var d_urbsh_90_`anio' "Dummy urban share for `anio' 90% urban pop. cutoff"
}

drop _merge
save `urbsh_zambia', replace

********************************************************************************
* Fixing missing observations using urban share country by country
********************************************************************************

* Notes: We use the closest year of shares to approximate target year. If two years are close to the target (before and after), we use an average of both.
* We create a new dataset for storing shares using an arbitrary dataset

* All OCCISCO
clear
save "processed_datasets\urban_shares_countries_occisco_all", replace emptyok


* All INDGEN
clear
save "processed_datasets\urban_shares_countries_indgen_all", replace emptyok


*-------------------------------------------------------------------------------
* Argentina - 1970
* Missing Year: 1970
* Used shares of: 1980 

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\argentina_`labr'_1970_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\argentina_`labr'_1970_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\argentina_`labr'_1970_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_argentina', keepusing(`share'_1980)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_1980
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_1980
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Argentina"
			gen year = 1970
			gen type_share = "`share'"
			gen year_share = "1980"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\argentina_`labr'_1970_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Benin - 1979
* Missing Year: 1979
* Used shares of: 1992 

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\benin_`labr'_1979_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\benin_`labr'_1979_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\benin_`labr'_1979_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_benin', keepusing(`share'_1992)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_1992
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_1992
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Benin"
			gen year = 1979
			gen type_share = "`share'"
			gen year_share = "1992"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\benin_`labr'_1979_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Burkina Faso - 1996
* Missing Year: 1996
* Used shares of: 2006 

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\burkinafaso_`labr'_1996_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\burkinafaso_`labr'_1996_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\burkinafaso_`labr'_1996_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_burkinafaso', keepusing(`share'_2006)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_2006
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_2006
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Burkina Faso"
			gen year = 1996
			gen type_share = "`share'"
			gen year_share = "2006"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\burkinafaso_`labr'_1996_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Cameroon - 1976
* Missing Year: 1976
* Used shares of: 1987 

foreach fil in all{
	
    foreach labr in occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\cameroon_`labr'_1976_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\cameroon_`labr'_1976_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\cameroon_`labr'_1976_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_cameroon', keepusing(`share'_1987)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_1987
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_1987
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Cameroon"
			gen year = 1976
			gen type_share = "`share'"
			gen year_share = "1987"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\cameroon_`labr'_1976_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* China - 1982
* Missing Year: 1982
* Used shares of: 2000

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\china_`labr'_1982_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\china_`labr'_1982_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\china_`labr'_1982_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_china', keepusing(`share'_2000)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_2000
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_2000
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "China"
			gen year = 1982
			gen type_share = "`share'"
			gen year_share = "2000"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\china_`labr'_1982_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* China - 1990
* Missing Year: 1990
* Used shares of: 2000

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\china_`labr'_1990_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\china_`labr'_1990_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\china_`labr'_1990_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_china', keepusing(`share'_2000)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_2000
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_2000
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "China"
			gen year = 1990
			gen type_share = "`share'"
			gen year_share = "2000"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\china_`labr'_1990_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Dominican Republic - 1981
* Missing Year: 1981
* Used shares of: 1970

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\dominicanrepublic_`labr'_1981_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\dominicanrepublic_`labr'_1981_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\dominicanrepublic_`labr'_1981_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_dominicanrepublic', keepusing(`share'_1970)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_1970
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_1970
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Dominican Republic"
			gen year = 1981
			gen type_share = "`share'"
			gen year_share = "1970"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\dominicanrepublic_`labr'_1981_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Ecuador - 1974
* Missing Year: 1974
* Used shares of: 1962

foreach fil in all{
	
    foreach labr in occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\ecuador_`labr'_1974_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\ecuador_`labr'_1974_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\ecuador_`labr'_1974_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_ecuador', keepusing(`share'_1962)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_1962
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_1962
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Ecuador"
			gen year = 1974
			gen type_share = "`share'"
			gen year_share = "1962"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\ecuador_`labr'_1974_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Ecuador - 1982
* Missing Year: 1982
* Used shares of: 1990

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\ecuador_`labr'_1982_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\ecuador_`labr'_1982_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\ecuador_`labr'_1982_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_ecuador', keepusing(`share'_1990)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_1990
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_1990
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Ecuador"
			gen year = 1982
			gen type_share = "`share'"
			gen year_share = "1990"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\ecuador_`labr'_1982_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* El Salvador - 1992
* Missing Year: 1992
* Used shares of: 2007

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\elsalvador_`labr'_1992_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\elsalvador_`labr'_1992_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\elsalvador_`labr'_1992_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_elsalvador', keepusing(`share'_2007)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_2007
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_2007
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "El Salvador"
			gen year = 1992
			gen type_share = "`share'"
			gen year_share = "2007"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\elsalvador_`labr'_1992_`fil'.dta
 	}
}


*-------------------------------------------------------------------------------
* Fiji - 1976
* Missing Year: 1976
* Used shares of: 1986

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\fiji_`labr'_1976_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\fiji_`labr'_1976_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\fiji_`labr'_1976_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_fiji', keepusing(`share'_1986)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_1986
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_1986
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Fiji"
			gen year = 1976
			gen type_share = "`share'"
			gen year_share = "1986"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\fiji_`labr'_1976_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Fiji - 1996
* Missing Year: 1996
* Used shares of: 1986 and 2007 (average of both)

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\fiji_`labr'_1996_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\fiji_`labr'_1996_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\fiji_`labr'_1996_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_fiji', keepusing(`share'_1986 `share'_2007)
			keep if _merge == 3
			drop _merge
			
			* Generating average share
			gen `share'_avg_8607 = (`share'_1986+`share'_2007)/2
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_avg_8607
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_avg_8607 `share'_1986 `share'_2007
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Fiji"
			gen year = 1996
			gen type_share = "`share'"
			gen year_share = "1986/2007"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\fiji_`labr'_1996_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Haiti - 1982
* Missing Year: 1982
* Used shares of: 1971

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\haiti_`labr'_1982_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\haiti_`labr'_1982_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\haiti_`labr'_1982_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_haiti', keepusing(`share'_1971)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_1971
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_1971
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Haiti"
			gen year = 1982
			gen type_share = "`share'"
			gen year_share = "1971"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\haiti_`labr'_1982_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Honduras - 1961
* Missing Year: 1961
* Used shares of: 1988 (1974 urban/rural data seems to have problems)

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\honduras_`labr'_1961_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\honduras_`labr'_1961_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\honduras_`labr'_1961_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_honduras', keepusing(`share'_1988)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_1988
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_1988
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Honduras"
			gen year = 1961
			gen type_share = "`share'"
			gen year_share = "1988"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\honduras_`labr'_1961_`fil'.dta
 	}
}


*-------------------------------------------------------------------------------
* Mali - 1987
* Missing Year: 1987
* Used shares of: 1998

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\mali_`labr'_1987_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\mali_`labr'_1987_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\mali_`labr'_1987_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_mali', keepusing(`share'_1998)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_1998
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_1998
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Mali"
			gen year = 1987
			gen type_share = "`share'"
			gen year_share = "1998"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\mali_`labr'_1987_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Nicaragua - 1971 and 1995
* Missing Year: 1971 and 1995
* Used shares of: 2005

foreach ye in 1971 1995{

	foreach fil in all{
		
		foreach labr in indgen occisco{
			
			* Importing
			import delimited raw_datasets\IPUMS_labor\urban-rural\nicaragua_`labr'_`ye'_`fil'.csv, encoding(UTF-8) clear
			
			* subdist is loaded as strL which prevents merging, this fixes the problem
			gen subdist2 = subdist
			drop subdist
			rename subdist2 subdist
			
			* Save
			save raw_datasets\IPUMS_labor\urban-rural\nicaragua_`labr'_`ye'_`fil', replace
			
			* Loop through share files
			foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
				
				* Import data
				use raw_datasets\IPUMS_labor\urban-rural\nicaragua_`labr'_`ye'_`fil', clear
				
				* Merging
				merge 1:1 subdist using `urbsh_nicaragua', keepusing(`share'_2005)
				keep if _merge == 3
				drop _merge
				
				* Applying within shares
				foreach w of varlist _all{
					replace `w' = `w'*`share'_2005
				}
					
				* Cleaning and collapsing
				drop type_var subdist `share'_2005
				
				* Collapsing
				collapse (sum) _all
				
				* Country and Year
				gen country = "Nicaragua"
				gen year = `ye'
				gen type_share = "`share'"
				gen year_share = "2005"
				
				* Appending
				append using "processed_datasets\urban_shares_countries_`labr'_all"
				save "processed_datasets\urban_shares_countries_`labr'_all", replace
			}
			erase raw_datasets\IPUMS_labor\urban-rural\nicaragua_`labr'_`ye'_`fil'.dta
		}
	}

}

*-------------------------------------------------------------------------------
* Panama - 1990
* Missing Year: 1990
* Used shares of: 1980 and 2000 (average of both)

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\panama_`labr'_1990_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\panama_`labr'_1990_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\panama_`labr'_1990_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_panama', keepusing(`share'_1980 `share'_2000)
			keep if _merge == 3
			drop _merge
			
			* Generating average share
			gen `share'_avg_0000 = (`share'_1980+`share'_2000)/2
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_avg_0000
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_avg_0000 `share'_1980 `share'_2000
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Panama"
			gen year = 1990
			gen type_share = "`share'"
			gen year_share = "1980/2000"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\panama_`labr'_1990_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Philippines - 1995
* Missing Year: 1995
* Used shares of: 1990

foreach ye in 1995{

	foreach fil in all{
		
		foreach labr in indgen{
			
			* Importing
			import delimited raw_datasets\IPUMS_labor\urban-rural\philippines_`labr'_`ye'_`fil'.csv, encoding(UTF-8) clear
			
			* subdist is loaded as strL which prevents merging, this fixes the problem
			gen subdist2 = subdist
			drop subdist
			rename subdist2 subdist
			
			* Save
			save raw_datasets\IPUMS_labor\urban-rural\philippines_`labr'_`ye'_`fil', replace
			
			* Loop through share files
			foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
				
				* Import data
				use raw_datasets\IPUMS_labor\urban-rural\philippines_`labr'_`ye'_`fil', clear
				
				* Merging
				merge 1:1 subdist using `urbsh_philippines', keepusing(`share'_1990)
				keep if _merge == 3
				drop _merge
				
				* Applying within shares
				foreach w of varlist _all{
					replace `w' = `w'*`share'_1990
				}
					
				* Cleaning and collapsing
				drop type_var subdist `share'_1990
				
				* Collapsing
				collapse (sum) _all
				
				* Country and Year
				gen country = "Philippines"
				gen year = `ye'
				gen type_share = "`share'"
				gen year_share = "1990"
				
				* Appending
				append using "processed_datasets\urban_shares_countries_`labr'_all"
				save "processed_datasets\urban_shares_countries_`labr'_all", replace
			}
			erase raw_datasets\IPUMS_labor\urban-rural\philippines_`labr'_`ye'_`fil'.dta
		}
	}

}

*-------------------------------------------------------------------------------
* Philippines - 2000 and 2010
* Missing Year: 2000 and 2010
* Used shares of: 1990

foreach ye in 2000 2010{

	foreach fil in all{
		
		foreach labr in indgen occisco{
			
			* Importing
			import delimited raw_datasets\IPUMS_labor\urban-rural\philippines_`labr'_`ye'_`fil'.csv, encoding(UTF-8) clear
			
			* subdist is loaded as strL which prevents merging, this fixes the problem
			gen subdist2 = subdist
			drop subdist
			rename subdist2 subdist
			
			* Save
			save raw_datasets\IPUMS_labor\urban-rural\philippines_`labr'_`ye'_`fil', replace
			
			* Loop through share files
			foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
				
				* Import data
				use raw_datasets\IPUMS_labor\urban-rural\philippines_`labr'_`ye'_`fil', clear
				
				* Merging
				merge 1:1 subdist using `urbsh_philippines', keepusing(`share'_1990)
				keep if _merge == 3
				drop _merge
				
				* Applying within shares
				foreach w of varlist _all{
					replace `w' = `w'*`share'_1990
				}
					
				* Cleaning and collapsing
				drop type_var subdist `share'_1990
				
				* Collapsing
				collapse (sum) _all
				
				* Country and Year
				gen country = "Philippines"
				gen year = `ye'
				gen type_share = "`share'"
				gen year_share = "1990"
				
				* Appending
				append using "processed_datasets\urban_shares_countries_`labr'_all"
				save "processed_datasets\urban_shares_countries_`labr'_all", replace
			}
			erase raw_datasets\IPUMS_labor\urban-rural\philippines_`labr'_`ye'_`fil'.dta
		}
	}

}

*-------------------------------------------------------------------------------
* Senegal - 1988
* Missing Year: 1988
* Used shares of: 2002

foreach fil in all{
	
    foreach labr in indgen occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\Senegal_`labr'_1988_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\Senegal_`labr'_1988_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\Senegal_`labr'_1988_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_senegal', keepusing(`share'_2002)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_2002
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_2002
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Senegal"
			gen year = 1988
			gen type_share = "`share'"
			gen year_share = "2002"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\Senegal_`labr'_1988_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Tanzania - 1988
* Missing Year: 1988
* Used shares of: 2002

foreach fil in all{
	
    foreach labr in occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\tanzania_`labr'_1988_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\tanzania_`labr'_1988_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\tanzania_`labr'_1988_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_tanzania', keepusing(`share'_2002)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_2002
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_2002
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Tanzania"
			gen year = 1988
			gen type_share = "`share'"
			gen year_share = "2002"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\tanzania_`labr'_1988_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------
* Uruguay - 1996 and 2006
* Missing Year: 1996 and 2006
* Used shares of: 1985

foreach ye in 1996 2006{

	foreach fil in all{
		
		foreach labr in indgen occisco{
			
			* Importing
			import delimited raw_datasets\IPUMS_labor\urban-rural\uruguay_`labr'_`ye'_`fil'.csv, encoding(UTF-8) clear
			
			* subdist is loaded as strL which prevents merging, this fixes the problem
			gen subdist2 = subdist
			drop subdist
			rename subdist2 subdist
			
			* Save
			save raw_datasets\IPUMS_labor\urban-rural\uruguay_`labr'_`ye'_`fil', replace
			
			* Loop through share files
			foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
				
				* Import data
				use raw_datasets\IPUMS_labor\urban-rural\uruguay_`labr'_`ye'_`fil', clear
				
				* Merging
				merge 1:1 subdist using `urbsh_uruguay', keepusing(`share'_1985)
				keep if _merge == 3
				drop _merge
				
				* Applying within shares
				foreach w of varlist _all{
					replace `w' = `w'*`share'_1985
				}
					
				* Cleaning and collapsing
				drop type_var subdist `share'_1985
				
				* Collapsing
				collapse (sum) _all
				
				* Country and Year
				gen country = "Uruguay"
				gen year = `ye'
				gen type_share = "`share'"
				gen year_share = "1985"
				
				* Appending
				append using "processed_datasets\urban_shares_countries_`labr'_all"
				save "processed_datasets\urban_shares_countries_`labr'_all", replace
			}
			erase raw_datasets\IPUMS_labor\urban-rural\uruguay_`labr'_`ye'_`fil'.dta
		}
	}

}

*-------------------------------------------------------------------------------
* Zambia - 2010
* Missing Year: 2010
* Used shares of: 2000

foreach fil in all{
	
    foreach labr in occisco{
	    
		* Importing
		import delimited raw_datasets\IPUMS_labor\urban-rural\zambia_`labr'_2010_`fil'.csv, encoding(UTF-8) clear
		
		* subdist is loaded as strL which prevents merging, this fixes the problem
		gen subdist2 = subdist
		drop subdist
		rename subdist2 subdist
		
		* Save
		save raw_datasets\IPUMS_labor\urban-rural\zambia_`labr'_2010_`fil', replace
		
		* Loop through share files
		foreach share in wit_urbsh bet_urbsh d_urbsh_80 d_urbsh_90 {
		    
			* Import data
			use raw_datasets\IPUMS_labor\urban-rural\zambia_`labr'_2010_`fil', clear
			
			* Merging
			merge 1:1 subdist using `urbsh_zambia', keepusing(`share'_2000)
			keep if _merge == 3
			drop _merge
			
			* Applying within shares
			foreach w of varlist _all{
				replace `w' = `w'*`share'_2000
			}
				
			* Cleaning and collapsing
			drop type_var subdist `share'_2000
			
			* Collapsing
			collapse (sum) _all
			
			* Country and Year
			gen country = "Zambia"
			gen year = 2010
			gen type_share = "`share'"
			gen year_share = "2000"
			
			* Appending
			append using "processed_datasets\urban_shares_countries_`labr'_all"
			save "processed_datasets\urban_shares_countries_`labr'_all", replace
		}
		erase raw_datasets\IPUMS_labor\urban-rural\zambia_`labr'_2010_`fil'.dta
 	}
}

*-------------------------------------------------------------------------------

* Togo
* Notes: Togo's 1960 census only contains urban sample, we obtain OCCISCO distribution for that year

foreach fil in all{
    import delimited raw_datasets\IPUMS_labor\urban-rural\togo_occisco_1960_`fil'.csv, encoding(UTF-8) clear
	
	drop type_var subdist
	gen country = "Togo"
	gen year = 1960
	
	save raw_datasets\IPUMS_labor\urban-rural\togo_occisco_1960_`fil', replace
	
	append using "processed_datasets\urban_shares_countries_occisco_all"
	save "processed_datasets\urban_shares_countries_occisco_all", replace
	
	erase raw_datasets\IPUMS_labor\urban-rural\togo_occisco_1960_`fil'.dta
}

